<template>
  <!-- 旅游菜单栏 -->
  <div class="menus-wrapper" @mouseleave="MouseLeaveItem()">
    <div
      class="menus"
      v-for="(item, index) in cityListItem"
      :key="index"
      @mouseover="MouseOverItem(index)"
    >
      <div class="menu-item">
        <span>{{ item.type }}</span>
      </div>
    </div>
    <div class="sub-menus">
      <ul>
        <li v-for="(item, index) in cityListSubItems" :key="index">
          <nuxt-link :to="`/post?city=` + item.city">
            <div class="city" @click="searchCity(item.city)">
              <i>{{ index + 1 }}</i>
              <strong>{{ item.city }}</strong>
              <span>{{ item.desc }}</span>
            </div>
          </nuxt-link>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { postList as getPostList } from "@/utils/apis/post";
import bus from "@/components/post/utils/postBus";
export default {
  data() {
    return {
      cityListItem: [],
      cityListSubItems: [],
    };
  },
  methods: {
    MouseOverItem(index) {
      this.cityListSubItems = this.cityListItem[index].children;
      document.querySelector(".sub-menus").style.display = "block";
    },
    MouseLeaveItem() {
      document.querySelector(".sub-menus").style.display = "";
    },
    async searchCity(city) {
      let res = await getPostList({ city });
      bus.$emit("postList", res);
    },
  },
  mounted() {
    this.$axios({
      url: "/posts/cities",
    }).then((res) => {
      // console.log(res);
      this.cityListItem = res.data.data;
    });

    // console.log(this.$route.query);
    // let { city } = this.$route.query;
    // console.log({ city });

    // let res = await postList({ city });
    // console.log(res);
  },
};
</script>

<style lang="less" scoped>
.menus {
  width: 260px;
  border: 1px solid #ddd;
  border-right: none;
  border-bottom: none;
  box-shadow: 0 0 1px #f5f5f5;
  z-index: 2;
  .menu-item {
    height: 40px;
    line-height: 40px;
    border-bottom: 1px solid #ddd;
    border-right: 1px solid #ddd;
    padding: 0 20px;
    font-size: 14px;
    position: relative;
    z-index: 2;

    &:after {
      display: block;
      content: "";
      width: 10px;
      height: 10px;
      border-right: 1px solid #999;
      border-top: 1px solid #999;
      transform: rotate(45deg);
      position: absolute;
      right: 20px;
      top: 15px;
    }
  }
}
.sub-menus {
  display: none;
  position: absolute;
  left: 260px;
  top: 0;
  width: 350px;
  padding: 10px 20px;
  box-sizing: border-box;
  background: #fff;
  border: 1px solid #ddd;
  ul {
    li {
      font-size: 14px;
      line-height: 1.5;
      .city {
        text-decoration: none;
        i {
          color: orange;
          font-size: 24px;
          font-style: italic;
        }
        strong {
          margin: 0 10px;
          color: orange;
          font-weight: 400;
          &:hover {
            text-decoration: underline;
          }
        }
        span {
          color: #999;
          &:hover {
            text-decoration: underline;
          }
        }
      }
    }
  }
}
</style>